package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.UserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.SQLException;

public class CompleteUserView extends JFrame {
    public static void main(String[] args) {
        new CompleteUserView();
    }

    JLabel nameLabel=new JLabel("个人信息",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    /**
     *  添加标签和文本框
     */
    JLabel nickNameLabel=new JLabel("昵称:");
    JTextField nickNameTxt=new JTextField();
    JLabel genderLabel=new JLabel("性别:");
    JTextField genderTxt=new JTextField();
    JLabel birthLabel=new JLabel("出生日期:");
    JTextField birthTxt=new JTextField();
    JLabel numberLabel=new JLabel("电话号码:");
    JTextField numberTxt=new JTextField();
    JLabel  modifyPasswordLabel=new JLabel("修改密码:");
    JTextField modifyPasswordTxt=new JTextField();

    private String nickName=null;
    private String gender=null;
    private String birth=null;
    private String number=null;
    private String password=null;


    //按钮
    JButton confirmButton = new JButton("确认");
    JButton returnButton=new JButton("返回");

    SystemTray systemTray;
    TrayIcon trayIcon;

    Object[] arr=null;

    //控制层对象
    UserController userController=new UserController();
    public CompleteUserView(){
        super("个人信息完善");

        //获取面板内容
        Container container=getContentPane();
        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(0,80));
        //规定标签字体,文本框大小
        Font  centerFont=new Font("楷体",Font.PLAIN,20);
        //昵称
        nickNameLabel.setFont(centerFont);
        nickNameTxt.setPreferredSize(new Dimension(200,30));
        //性别
        genderLabel.setFont(centerFont);
        genderTxt.setPreferredSize(new Dimension(200,30));
        //出生日期
        birthLabel.setFont(centerFont);
        birthTxt.setPreferredSize(new Dimension(200,30));
        //电话号码
        numberLabel.setFont(centerFont);
        numberTxt.setPreferredSize(new Dimension(200,30));
        //修改密码
        modifyPasswordLabel.setFont(centerFont);
        modifyPasswordTxt.setPreferredSize(new Dimension(200,30));
        //按钮
        confirmButton.setFont(centerFont);
        returnButton.setFont(centerFont);

        //把组件加入面板
        centerPanel.add(nickNameLabel);
        centerPanel.add(nickNameTxt);
        centerPanel.add(genderLabel);
        centerPanel.add(genderTxt);
        centerPanel.add(birthLabel);
        centerPanel.add(birthTxt);
        centerPanel.add(numberLabel);
        centerPanel.add(numberTxt);
        centerPanel.add(modifyPasswordLabel);
        centerPanel.add(modifyPasswordTxt);

        //增加按钮事件
        centerPanel.add(confirmButton);
        centerPanel.add(returnButton);

        //弹簧布局
        layout(container);

        //增加系统托盘
        addTray();

        //自定义图标
        URL imgUrl=LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600,400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);

        //初始化个文本框
        initialize();


        //"确认"按钮监听器
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nickName=getNickNameTxt().getText();
                gender=getGenderTxt().getText();
                birth=getBirthTxt().getText();
                number=getNumberTxt().getText();
                password=getModifyPasswordTxt().getText();
                boolean result = userController.completeUser(nickName, gender, birth, number, password);
                if(result){
                    JOptionPane.showMessageDialog(null, "修改成功！");
                }
            }
        });
        //"返回"按钮监听器
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new WordBookView();

            }
        });


    }

    private void initialize() {

        try {
            arr = userController.pInformation();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        nickNameTxt.setText((String) arr[0]);
        genderTxt.setText((String) arr[1]);
        birthTxt.setText((String) arr[2]);
        numberTxt.setText((String) arr[3]);
    }
    //get&set

    public JTextField getNickNameTxt() {
        return nickNameTxt;
    }

    public void setNickNameTxt(JTextField nickNameTxt) {
        this.nickNameTxt = nickNameTxt;
    }

    public JTextField getGenderTxt() {
        return genderTxt;
    }

    public void setGenderTxt(JTextField genderTxt) {
        this.genderTxt = genderTxt;
    }

    public JTextField getBirthTxt() {
        return birthTxt;
    }

    public void setBirthTxt(JTextField birthTxt) {
        this.birthTxt = birthTxt;
    }

    public JTextField getNumberTxt() {
        return numberTxt;
    }

    public void setNumberTxt(JTextField numberTxt) {
        this.numberTxt = numberTxt;
    }

    public JTextField getModifyPasswordTxt() {
        return modifyPasswordTxt;
    }

    public void setModifyPasswordTxt(JTextField modifyPasswordTxt) {
        this.modifyPasswordTxt = modifyPasswordTxt;
    }

    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局nickNameLabel
        Spring childWidth=Spring.sum(Spring.sum(Spring.width(nickNameLabel),Spring.width(nickNameTxt)),
                Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,nickNameLabel,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,nickNameLabel,0,SpringLayout.NORTH,centerPanel);
        //nickNameTxt
        springLayout.putConstraint(SpringLayout.WEST,nickNameTxt,20,SpringLayout.EAST,nickNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,nickNameTxt,0,SpringLayout.NORTH,nickNameLabel);
        //genderLabel
        springLayout.putConstraint(SpringLayout.EAST,genderLabel,0,SpringLayout.EAST,nickNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,genderLabel,20,SpringLayout.SOUTH,nickNameLabel);
        //genderField
        springLayout.putConstraint(SpringLayout.WEST,genderTxt,20,SpringLayout.EAST,genderLabel);
        springLayout.putConstraint(SpringLayout.NORTH,genderTxt,0,SpringLayout.NORTH,genderLabel);
        //birthLabel
        springLayout.putConstraint(SpringLayout.EAST,birthLabel,0,SpringLayout.EAST,genderLabel);
        springLayout.putConstraint(SpringLayout.NORTH,birthLabel,20,SpringLayout.SOUTH,genderLabel);
        //birthField
        springLayout.putConstraint(SpringLayout.WEST,birthTxt,20,SpringLayout.EAST,birthLabel);
        springLayout.putConstraint(SpringLayout.NORTH,birthTxt,0,SpringLayout.NORTH,birthLabel);
        //numberLabel
        springLayout.putConstraint(SpringLayout.EAST,numberLabel,0,SpringLayout.EAST,birthLabel);
        springLayout.putConstraint(SpringLayout.NORTH,numberLabel,20,SpringLayout.SOUTH,birthLabel);
        //numberField
        springLayout.putConstraint(SpringLayout.WEST,numberTxt,20,SpringLayout.EAST,numberLabel);
        springLayout.putConstraint(SpringLayout.NORTH,numberTxt,0,SpringLayout.NORTH,numberLabel);
        //modifyPasswordLabel
        springLayout.putConstraint(SpringLayout.EAST,modifyPasswordLabel,0,SpringLayout.EAST,numberLabel);
        springLayout.putConstraint(SpringLayout.NORTH,modifyPasswordLabel,20,SpringLayout.SOUTH,numberLabel);
        //modifyPasswordField
        springLayout.putConstraint(SpringLayout.WEST,modifyPasswordTxt,20,SpringLayout.EAST,modifyPasswordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,modifyPasswordTxt,0,SpringLayout.NORTH,modifyPasswordLabel);



        //loginButton
        springLayout.putConstraint(SpringLayout.WEST,confirmButton,70,SpringLayout.WEST, modifyPasswordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,confirmButton,20,SpringLayout.SOUTH,modifyPasswordLabel);
        //registerButton
        springLayout.putConstraint(SpringLayout.WEST,returnButton,50,SpringLayout.EAST,confirmButton);
        springLayout.putConstraint(SpringLayout.NORTH,returnButton,0,SpringLayout.NORTH,confirmButton);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    /**
     * 增加系统托盘
     */
    private void addTray() {
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();
            URL imgUrl=LoginView.class.getClassLoader().getResource("tray.png");
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片大小自动缩放
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    CompleteUserView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        CompleteUserView.this.setExtendedState(JFrame.NORMAL);
                    }
                   CompleteUserView.this.setVisible(true);
                }
            });
        }
    }
}
